package io.finer.erp.jeecg.bas.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class RemoteUtils {
	
	private final static String remote_url="http://jiqie.zhenbi.com/5/re13.php";
	
//	public static void main(String[] args) {
//		Gson json=new Gson();
//		JSONObject jsonObject=new JSONObject();
//		RequestInfo ri=new RequestInfo();
//		jsonObject.put("id", 1080);
//		jsonObject.put("zhenbi","20191123");
//		jsonObject.put("id1", 0);
//		jsonObject.put("id2", 270);
//		jsonObject.put("id3", 280);
//		jsonObject.put("id4", 290);
//		jsonObject.put("id5", 300);
//		jsonObject.put("id6", 260);
//		jsonObject.put("id8", 250);
//		jsonObject.put("id9", 240);
//		jsonObject.put("id10", 230);
//		jsonObject.put("id11", 240);
//		jsonObject.put("id12", 220);
//		
////		ri.setId(1080);
////		ri.setZhenbi("20191123");
////		ri.setId(1080);
////		ri.setId1(0);
////		ri.setId2(270);
////		ri.setId3(270);
////		ri.setId4(270);
////		ri.setId5(270);
////		ri.setId7(270);
////		ri.setId8(270);
////		ri.setId9(270);
////		ri.setId10(270);
////		ri.setId11(270);
////		ri.setId12(270);
//		JSONObject result=RestUtil.post(remote_url, jsonObject);
//		System.out.println(JSONObject.toJSON(result));
//	}
	
	public static String makePostCall(RequestInfo ri,Integer num){
		String res="";
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(remote_url);

            // add header
            List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
            urlParameters.add(new BasicNameValuePair("id", num+""));
            urlParameters.add(new BasicNameValuePair("zhenbi", "20191123"));
            urlParameters.add(new BasicNameValuePair("id1", "0"));
            if(null!=ri) {
            	urlParameters.add(new BasicNameValuePair("id2", ri.getId2()==0?"":ri.getId2()+""));
                urlParameters.add(new BasicNameValuePair("id3", ri.getId3()==0?"":ri.getId3()+""));
                urlParameters.add(new BasicNameValuePair("id4", ri.getId4()==0?"":ri.getId4()+""));
                urlParameters.add(new BasicNameValuePair("id5", ri.getId5()==0?"":ri.getId5()+""));
                urlParameters.add(new BasicNameValuePair("id7", ri.getId7()==0?"":ri.getId7()+""));
                urlParameters.add(new BasicNameValuePair("id8", ri.getId8()==0?"":ri.getId8()+""));
                urlParameters.add(new BasicNameValuePair("id9", ri.getId9()==0?"":ri.getId9()+""));
                urlParameters.add(new BasicNameValuePair("id10", ri.getId10()==0?"":ri.getId10()+""));
                urlParameters.add(new BasicNameValuePair("id11", ri.getId11()==0?"":ri.getId11()+""));
                urlParameters.add(new BasicNameValuePair("id12", ri.getId12()==0?"":ri.getId12()+""));
                res="<tr>";
                if(ri.getId2()!=0) {
                	res+="<th>"+ri.getId2()+"</th>";
                }
                if(ri.getId3()!=0) {
                	res+="<th>"+ri.getId3()+"</th>";
                }
                if(ri.getId4()!=0) {
                	res+="<th>"+ri.getId4()+"</th>";
                }
                if(ri.getId5()!=0) {
                	res+="<th>"+ri.getId5()+"</th>";
                }
                if(ri.getId7()!=0) {
                	res+="<th>"+ri.getId7()+"</th>";
                }
                if(ri.getId8()!=0) {
                	res+="<th>"+ri.getId8()+"</th>";
                }
                if(ri.getId9()!=0) {
                	res+="<th>"+ri.getId9()+"</th>";
                }
                if(ri.getId10()!=0) {
                	res+="<th>"+ri.getId10()+"</th>";
                }
                if(ri.getId11()!=0) {
                	res+="<th>"+ri.getId11()+"</th>";
                }
                if(ri.getId12()!=0) {
                	res+="<th>"+ri.getId12()+"</th>";
                }
                res+="<th>成品</th><th>切边</th><th>成品率%</th></tr>";
//                res="<tr><th>"+ri.getId2()+"</th><th>"+ri.getId3()+"</th><th>"+ri.getId4()+"</th><th>"+ri.getId5()+"</th><th>"+ri.getId7()+"</th><th>"+ri.getId8()+"</th><th>"+ri.getId9()+"</th><th>"+ri.getId10()+"</th><th>"+ri.getId11()+"</th><th>"+ri.getId12()+"</th><th>成品</th><th>切边</th><th>成品率%</th></tr>";
                System.out.println(res);
            }
            post.setEntity(new UrlEncodedFormEntity(urlParameters));
            HttpResponse response = client.execute(post);
            System.out.println("\nSending 'POST' request to URL : " + remote_url);
            System.out.println("Post parameters : " + post.getEntity());
            System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer result = new StringBuffer();
            String line = "";
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            System.out.println(result.toString());
            Pattern pattern = Pattern.compile("((?is)<tr>(.+?)</tr>)");
            Matcher m = pattern.matcher(result.toString());
//            System.out.println("匹配文本==="+m.toString());
//            String[] s = new String[2];
            int k=0;
            while (m.find()) {
//                s[0] = m.group(1);
                if(k!=0) {
                	if(num<=1080) {
                		if(m.group(1).indexOf("1080")>0||m.group(1).indexOf("1070")>0) {
                        	System.out.println(m.group(1));
                        	res+="\r\n"+m.group(1);
                        }
                	}else {
                		if(m.group(1).indexOf("1180")>0||m.group(1).indexOf("1170")>0) {
                        	System.out.println(m.group(1));
                        	res+="\r\n"+m.group(1);
                        }
                	}
                }
                k++;
            }
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
	
	public static void main(String[] args) {
//		makePostCall();
	}

}
